<template>
  <div class="login-container">
    <el-form ref="registerForm" class="login-form" label-width="100px" :model="form" :rules="rules">
      <div class="title-container">
        <img src="/logo.png" alt="">
      </div>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item prop="captcha" label="验证码" class="captcha-container">
        <div class="captcha">
          <img :src="code.captcha" alt="" @click="updateCaptcha">
        </div>
        <el-input v-model="form.captcha" class="w300" placeholder="请输入验证码" />
      </el-form-item>
      <el-form-item prop="nickname" label="昵称">
        <el-input v-model="form.nickname" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item prop="passwd" label="密码">
        <el-input v-model="form.passwd" type="password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item prop="repasswd" label="确认密码">
        <el-input v-model="form.repasswd" type="password" placeholder="请再次输入密码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native="onRegister">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import md5 from 'md5'
export default {
  layout: 'login',
  data () {
    return {
      form: {
        email: '2535924382@qq.com',
        captcha: '',
        nickname: '小白',
        passwd: '123456',
        repasswd: '123456'
      },
      rules: {
        email: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' },
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'change' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'change' }
        ],
        passwd: [
          { required: true, pattern: /^[\w_-]{6,12}$/g, message: '请输入6~12位密码', trigger: 'change' }
        ],
        repasswd: [
          { required: true, message: '请再次输入密码', trigger: 'change' },
          { validator: (rule, value, callback) => {
            if (value !== this.form.passwd) {
              callback(new Error('两次输入密码不一致'))
            }
            callback()
          }
          }
        ]
      },
      code: {
        captcha: '/api/captcha?+t=' + new Date().getTime(),
      }
    }
  },
  methods: {
    onRegister () {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          const obj = {
            email: this.form.email,
            captcha: this.form.captcha,
            nickname: this.form.nickname,
            passwd: md5(this.form.passwd)
          }
          const ret = await this.$http.post('/user/register', obj)
          if (ret.code === 0) {
            this.$alert('注册成功', '成功', {
              confirmButtonText: '去登录',
              callback: () => {
                this.$router.push('/login')
              }
            })
          } else {
            this.$message.error(ret.message)
          }
        } else {
          console.log('校验失败!');
        }
      })
    },
    updateCaptcha () {
      this.code.captcha = '/api/captcha?+t=' + new Date().getTime()
    }
  }
}
</script>

<style scoped lang="stylus">

</style>